package com.foxmail.yusp75.myexam;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class JudgeFragment extends Fragment {
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;
    private String c_anwser;
    private RadioGroup judge_rg;

    public JudgeFragment() {
    }

    public static JudgeFragment newInstance(String param1, String param2) {
        JudgeFragment fragment = new JudgeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_judge, container, false);
        final RadioButton right = view.findViewById(R.id.judge_right);
        final RadioButton wrong = view.findViewById(R.id.judge_wrong);
        judge_rg = view.findViewById(R.id.judge_rg);
        judge_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                if (i == right.getId())
                    c_anwser = "√";
                if (i == wrong.getId())
                    c_anwser = "×";

                if (mListener != null)
                    mListener.onFragmentInteraction(c_anwser);
            }
        });
        return view;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public void setmListener(JudgeFragment.OnFragmentInteractionListener listener) {
        mListener = listener;
    }

    public void reset() {
        if (judge_rg != null)
            judge_rg.clearCheck();
    }

    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(String c);
    }
}
